<?php
require_once dirname(__FILE__) . '/config.php';

if($_POST) {
	$path = $_POST['Path'];
	$content = $_POST['Content'];
	
	$prefix = 'http://' . $_SERVER['HTTP_HOST'];
	$uri = $_SERVER['REQUEST_URI'];
	if(substr($uri, 0, 9) != '/edit.php') {
		$subdir = substr($uri, 0, strpos($uri, '/edit.php'));
	}
	$prefix = $prefix . $subdir . '/';
	
	list($dir, $filename) = path2dirfile($path, true);
	file_put_contents($filename, $content);
	$content = content($path, $prefix);
	list($dir, $filename) = path2dirfile($path, true, HTML_DIR);
	file_put_contents($filename, $content);
	if($path == 'index') {
		header('Location: '.$prefix.'index.html');
	} else {
		header('Location: '.$prefix.'html/' . $path . '.html');
	}
	die();
} else {
	$path = pathfromreq();
	list($dir, $filename) = path2dirfile($path, false);
	$content = file_get_contents($filename);
}
?>

<form method="post">
<p>Path</p>
<p><input name="Path" value="<?php echo $path; ?>" ></p>
<p>Content</p>
<p>
<textarea name="Content" rows="20" cols="80"><?php echo $content; ?></textarea>
</p>
<p><input type="submit"></p>
</form>